<template>
  <page-layout>
    <div slot="toolbar">
      <el-button size="small" round type="info" @click="goBack">返回视频管理</el-button>
      <el-button size="small" round type="primary" @click="newVideoQuestion">新增练习题</el-button>
    </div>
    <div class="card-header">练习题管理</div>
    <el-card class="box-card ">
      <el-card class="box-card">
      <el-row>
        <el-col :span="24">
          <el-form ref="searchVideoQuestionCondition" :model="searchVideoQuestionCondition" label-width="80px" inline>
            <el-form-item label="标题">
              <el-input v-model="searchVideoQuestionCondition.title" style="width: 300px;"/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="search(1)">查询</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      </el-card>
      <el-card class="box-card">
            <el-table
              :data="videoQuestionList.content" border stripe
              style="width: 100%">
              <el-table-column type="expand">
                <template slot-scope="props">
                  <el-form label-position="left" inline class="demo-table-expand">
                    <el-form-item label="题目">
                      <span>{{ props.row.title }}</span>
                    </el-form-item>
                    <el-form-item label="分数">
                      <span>{{ props.row.score }}</span>
                    </el-form-item>
                    <el-form-item label="选项">
                      <span v-html="getOptions(props.row.options)"></span>
                    </el-form-item>
                    <el-form-item label="答案">
                      <span>{{ props.row.answer }}</span>
                    </el-form-item>
                    <el-form-item label="答题总数">
                      <span>{{ props.row.count }}</span>
                    </el-form-item>
                    <el-form-item label="正确率">
                      <span>{{ props.row.correctRate }}</span>
                    </el-form-item>
                    <el-form-item label="解析">
                      <span>{{ props.row.analysis }}</span>
                    </el-form-item>
                  </el-form>
                </template>
              </el-table-column>
              <el-table-column
                prop="id"
                label="编号"
                width="55">
              </el-table-column>

              <el-table-column prop="title" label="题目" ></el-table-column>
              <el-table-column
                prop="options"
                label="选项" >
                <template slot-scope="scope">
                  <span v-html="getOptions(scope.row.options)"></span>
                </template>
              </el-table-column>
              <el-table-column prop="answer" label="答案" ></el-table-column>
              <el-table-column prop="analysis" label="题目解析" ></el-table-column>
              <el-table-column prop="count" label="答题总数" ></el-table-column>
              <el-table-column prop="correctRate" label="正确率" ></el-table-column>
              <el-table-column prop="score" label="积分" ></el-table-column>
              <el-table-column label="操作" width="120">
                <template slot-scope="scope">
                  <el-button size="mini"  type="text" @click="editVideoQuestion(scope.$index, scope.row)" >编辑</el-button>
                  <el-button size="mini" type="text" @click="deleteVideoQuestion(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="mt10">
              <el-pagination
                background
                layout="prev, pager, next"
                :total="videoQuestionList.totalElements"
                :page-size="videoQuestionList.size"
                :current-page.sync="currentVideoQuestionPage" @current-change="getPageChange">
              </el-pagination>
            </div>
          </el-card>
    </el-card>
    <el-dialog :visible.sync="showVideoQuestionEditor" title="添加/编辑 练习题" width="70%">
      <video-question-editor @close="onVideoQuestionEditorClose" :video-id="videoId" :is-close="showVideoQuestionEditor"></video-question-editor>
    </el-dialog>
  </page-layout>
</template>
<script>
import PageLayout from "@/components/page-layout";
import VideoQuestionEditor from "./video-question-editor";
import { mapGetters } from "vuex";
import ImageCover from "@/components/image-cover";
import Status from "@/libs/status";
export default {
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.videoId = to.query.videoId;
      console.log(vm.videoId, 111);
      vm.getPageChange(1);
    });
  },
  created() {},
  mounted() {},
  data() {
    return {
      videoId: 0,
      currentVideoQuestionPage: 1,
      showVideoQuestionEditor: false,
      VideoQuestionStatus: Status.VideoQuestionStatus
    };
  },
  computed: {
    ...mapGetters({
      videoQuestionList: "videoQuestionList",
      searchVideoQuestionCondition: "searchVideoQuestionCondition"
    })
  },
  methods: {
    getOptions(options) {
      var tem = options.split("\n");
      var str = "";
      if (tem) {
        tem.map((item, index) => {
          str += "<p>" + item + "</p>";
        });
      }

      return str;
    },
    getPageChange(val) {
      this.searchVideoQuestionCondition.videoId = this.videoId;
      console.log(this.searchVideoQuestionCondition);
      var tem = Object.assign(
        { pageIndex: val - 1 },
        this.searchVideoQuestionCondition
      );
      this.$store.dispatch("getVideoQuestionPageS", tem).then(ret => {});
    },
    search(val) {
      this.getPageChange(1);
    },
    deleteVideoQuestion(row) {
      this.$confirm("确认删除当前活动?", "提示", {})
        .then(() => {
          this.$store
            .dispatch("deleteVideoQuestionS", { id: row.id })
            .then(ret => {
              this.getPageChange(1);
              this.$notify({
                title: "删除成功",
                type: "success"
              });
            });
        })
        .catch(() => {});
    },
    newVideoQuestion() {
      this.$store.commit("INIT_CURRENT_VIDEOQUESTION");
      this.showVideoQuestionEditor = true;
    },
    editVideoQuestion(index, row) {
      this.$store.dispatch("getVideoQuestionS", row.id);
      this.showVideoQuestionEditor = true;
    },
    onVideoQuestionEditorClose() {
      this.showVideoQuestionEditor = false;
      this.getPageChange(1);
    },
    save(row, status) {
      row.opt = status;
      this.$store.dispatch("addOrUpdateVideoQuestionS", row).then(ret => {
        this.$notify({
          title: "提交成功",
          type: "success"
        });
        this.getPageChange(1);
      });
    },
    goBack() {
      this.$router.push({
        name: "video"
      });
    },
    onShowPreviewClose() {
      this.showPreview = false;
      this.getPageChange(1);
    }
  },
  components: {
    PageLayout,
    VideoQuestionEditor,
    ImageCover
  }
};
</script>
<style scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
